import request from '@/utils/request'

/**
 * 检查手机号是否已注册
 * @param phone
 * @returns
 */
export const checkPhone = (phone: string) =>
    request({
        url: `/api/users/${phone}`,
        method: 'GET',
        data: {
            phone,
        },
    })

/**
 * 发送验证码
 * @param phone
 * @returns
 */
export const sendCode = (phone: string) =>
    request({
        url: `/api/users/${phone}/code`,
        method: 'GET',
        data: {
            phone,
        },
    })

/**
 * 验证码校验
 * @param phone
 * @param telcode
 * @returns
 */
export const verifyCode = (phone: string, telcode: string) =>
    request({
        url: `/api/users/${phone}/verifycode`,
        method: 'GET',
        data: {
            phone,
            telcode,
        },
    })

/**
 * 注册
 * @param phone
 * @param password
 * @returns
 */
export const register = (phone: string, password: string) =>
    request({
        url: '/api/users/register',
        method: 'POST',
        data: {
            phone,
            password,
        },
    })

/**
 * 登录
 * @param loginname
 * @param password
 * @returns
 */
export const login = (loginname: string, password: string) =>
    request({
        url: '/api/sessions/account',
        method: 'POST',
        data: {
            loginname,
            password,
        },
    })

/**
 * 获取用户信息
 * @param userid
 * @returns
 */
export const getUserInfo = (token:string) =>
    request({
        url: '/api/users/self',
        method: 'GET',
        // 请求拦截时，已经拦截了请求头中添加
        // token 的操作，所以每个单独的请求方法
        // 中可以不必再添加请求头中 token 的传递
        headers: {
            Authorization: 'Bearer '+ token
        },
    })
